package com.shushan.exercise;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.mixedexam.MixedExamActivity;
import com.shushan.mixedexam.bean.MutiExercisePaperBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.pages.newtype.StrangeWordsExerciseActivity;
import com.shushan.pages.newtype.bean.StrangeWordsListBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.ui.CustomDialog;
import com.shushan.util.Session;
import com.shushan.util.ToastUtils;
import com.shushan.util.ToolUtils;

/**
 * @ClassName: ContinueExerciseHelp
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-14
 */
public class ContinueExerciseHelp {


    public static Dialog showContinueDialog(final BaseActivity baseActivity, final int practiceType, final int paperId) {
        if(!baseActivity.isFinishing()){
            Dialog dialog = new CustomDialog.Builder(baseActivity)
                    .setTitle("温馨提示")
                    .setMessage("你还有未完成的练习是否继续完成 ？")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();

                            dropExercise(baseActivity,practiceType,paperId);
                        }
                    })
                    .setPositiveButton("去完成", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();

                            getContinueExercise(baseActivity,practiceType,paperId);
                        }
                    })
                    .create();

            dialog.show();

            return dialog;
        }else {
            return null;
        }

    }

    private static void dropExercise(final BaseActivity baseActivity,int practiceType, int paperId) {
        int childId = BabyApplication.getInstance().getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(baseActivity, UrlConst.BASE_URL).appendRegion(UrlConst.GIVE_UP_EXERCISE);
        UrlParse paramParse = new UrlCommParse(baseActivity)
                .putValue("child_id",childId)
                .putValue("practice_type", practiceType)
                .putValue("paper_id", paperId);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {

                    @Override
                    protected void onSuccess(String data) {

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        baseActivity.showNetError(netRetBean);
                    }
                });
    }

    private static void getContinueExercise(final BaseActivity baseActivity,final int practiceType, int paperId) {
        int childId = BabyApplication.getInstance().getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(baseActivity, UrlConst.BASE_URL).appendRegion(UrlConst.GET_CONTINUE_EXERCISE);
        UrlParse paramParse = new UrlCommParse(baseActivity)
                .putValue("child_id",childId)
                .putValue("practice_type", practiceType)
                .putValue("paper_id", paperId);

        baseActivity.showWaitDialog();

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<MutiExercisePaperBean>() {
                    @Override
                    protected void onCommon() {
                        baseActivity.dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(MutiExercisePaperBean mutiExercisePaperBean) {
                        if(mutiExercisePaperBean == null){
                            ToastUtils.showToast(baseActivity,"获取数据为空");
                            return;
                        }

                        Session.getSession().put("examPaperBean", mutiExercisePaperBean);

                        Intent intent = new Intent(baseActivity, MixedExamActivity.class);
                        intent.putExtra("examType", practiceType);
                        intent.putExtra(Constants.SCORE_RESULT_JUMP_TYPE, Constants.JUMP_TYPE_EXAM_CONTINUE);
                        baseActivity.startActivityForResultAllow(intent);
                    }
                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        baseActivity.showNetError(netRetBean);
                    }
                });
    }
}
